package com.example.padlock.view.service;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;

import com.example.padlock.R;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class MyApplication extends Application{
	
	private static Context mContext;
	
	private int mScreenHeight;
	private int mScreenWidth;
	private DisplayImageOptions defaultOptions;
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mContext=getApplicationContext();
		initImageLoader(this);
	}
	
	public static Context getApplicationGlobalContext(){
		return mContext;
	}
	
	/**
	 * 初始化ImageLoader
	 * 
	 * @param context
	 */
	public void initImageLoader(Context context) {
		mScreenHeight = getWallpaperDesiredMinimumHeight();
		mScreenWidth = getWallpaperDesiredMinimumWidth();

		defaultOptions = new DisplayImageOptions.Builder()

				// 之后需要修改此配置
				.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
				.bitmapConfig(Bitmap.Config.RGB_565)
				// .cacheInMemory(true)
				.showImageForEmptyUri(R.drawable.loading)
				.showImageOnFail(R.drawable.loading)
				.showStubImage(R.drawable.loading)

				.cacheOnDisc(true).build();
		/**
		 * This configuration tuning is custom. You can tune every option, you
		 * may tune some of them, or you can create default configuration by
		 * ImageLoaderConfiguration.createDefault(this); method.
		 */
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.memoryCache(new WeakMemoryCache())
				.threadPriority(Thread.MIN_PRIORITY)
				.threadPoolSize(3)
				// 设置图片Uri为空或是错误的时候显示的图片
				// 设置图片加载或解码过程中发生错误显示的图片
				.discCacheExtraOptions(400, 480, CompressFormat.JPEG, 90, null)
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				// .discCache(new UnlimitedDiscCache(new File(imageCacheDir)))
				.denyCacheImageMultipleSizesInMemory()
				// .memoryCache(new LruMemoryCache(1 * 1024 * 1024))
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.defaultDisplayImageOptions(defaultOptions)

				.writeDebugLogs()

				.build();
		System.out.println("================init======================");
		ImageLoader.getInstance().init(config);
	}


}
